import * as asyncWx from "../../utils/asyncWx.js";
Page({
    data:{
        tabs:[
            {
                id:0,
                name:"体验问题",
                isActive:true
            },
            {
                id:1,
                name:"商品、商家投诉",
                isActive:false
            }
        ],
        chooseImages:[],
        textareaValue:""
    },
    //提交到外网服务器返回的图片数组路径
    UploadImages:[],
    //点击子组件触发的父组件上的自定义事件
    clickChildTab(e){
        this.data.tabs.forEach((item)=>{
            if(item.id===e.detail.index){
                item.isActive=true;
            }else{
                item.isActive=false;
            };
        });
        // this.data.tabs.forEach(item=>item.id===e.detail.index?item.isActive=true:item.isActive=false);
        this.setData({
            tabs:this.data.tabs
        });
    },
    //点击添加照片事件监听
    handleAddImage(){
        wx.chooseImage({
            count:9,
            sizeType:["original","compressed"],
            sourceType:["album","camera"],
            success:(res)=>{
                // console.log(res);
                this.setData({
                    chooseImages:[...this.data.chooseImages,...res.tempFilePaths]
                });
            }
        });
    },
    //子组件传来的自定义删除事件监听
    handleRemoveImage(e){
        const index=e.detail;
        this.data.chooseImages.splice(index,1);
        this.setData({
            chooseImages:this.data.chooseImages
        });
    },
    //文本域输入事件监听
    handleTextareaInput(e){
        this.setData({
            textareaValue:e.detail.value
        });
    },
    //点击提交按钮事件监听
    handleSubmit(){
        const {textareaValue,chooseImages}=this.data;
        if(textareaValue.trim()){
            wx.showLoading({
                title:"正在上传中",
                mask:true
            });
            if(chooseImages.length>0){
                // chooseImages.forEach((item,index)=>{
                //     wx.uploadFile({
                //         filePath: item,
                //         name: 'file',
                //         url: 'https://images.ac.cn/Home/Index/UploadAction/',
                //         success:(res)=>{
                //             const url=JSON.parse(res.data).url;
                //             this.UploadImages.push(url);
                //             if(index===chooseImages.length-1){
                //                 wx.hideLoading();
                //                 console.log("图片与问题文字全部上传完毕");
                //                 this.setData({
                //                     chooseImages:[],
                //                     textareaValue:""
                //                 });
                //                 wx.navigateBack({
                //                     delta:1
                //                 });
                //             };
                //         }
                //     });
                // });
                wx.hideLoading();
                console.log("图片与问题文字全部上传完毕");
                this.setData({
                    chooseImages:[],
                    textareaValue:""
                });
                wx.navigateBack({
                    delta:1
                });
            }else{
                wx.hideLoading();
                console.log("只是提交问题文字");
                wx.navigateBack({
                    delta:1
                });
            };
        }else{
            asyncWx.showToast({
                title:"请输入问题",
                icon:"none"
            });
        };
    },
    previewCurrentImage(e){
        wx.previewImage({
            urls: this.data.chooseImages,
            current:e.currentTarget.dataset.src
        });
    }
});